﻿Public Class FormProductEditor

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        If Me.txtProductCode.Text = "" Then
            MsgBox("Please input product code")
            Me.txtProductCode.Focus()
            Exit Sub
        End If

        Cmd.Parameters("@CODE") = Me.txtProductCode.Text
        Cmd.Parameters("@Id") = Me.txtProductId.Text
        Dim count = Cmd.ExecuteScalar("SELECT COUNT(*) FROM Product  WHERE ProductCode=@CODE AND ProductId<>@Id;")
        If count > 0 Then
            MsgBox("Product code is already existed!")
            Me.txtProductCode.SelectAll()
            Me.txtProductCode.Focus()
            Exit Sub
        End If

        If Me.txtProductName.Text = "" Then
            MsgBox("Please input product name")
            Me.txtProductName.Focus()
            Exit Sub
        End If


        Dim tmp As Decimal
        Dim result = Decimal.TryParse(Me.txtPrice.Text, tmp)
        If result = False Then
            MsgBox("Plese input correct decimal value")
            Me.txtPrice.Focus()
            Exit Sub
        End If

        If Validator.IsDecimal(Me.txtPrice.Text) = False Then
            MsgBox("Plese input correct decimal value")
            Me.txtPrice.Focus()
            Exit Sub
        End If

        If Me.txtPrice.Text <= 0 Or Me.txtPrice.Text >= 100 Then
            MsgBox("Price must be between 0 and 100")
            Me.txtPrice.Focus()
            Exit Sub
        End If


        MsgBox("Save success!")



    End Sub

    Private Sub FormProductEditor_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class